package com.agger.harmonyosthemeseditor.userinterface;

import com.agger.harmonyosthemeseditor.ResourceTable;
import java.util.Optional;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.text.Font;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;

public class GeneralDialog
{
    private static final int MAX_BUTTON_NUMBER = 3;

    private static final int DIALOG_WIDTH_VP = 298;

    private final String[] mButtonTexts = new String[MAX_BUTTON_NUMBER];

    private final int[] mButtonTypes = new int[MAX_BUTTON_NUMBER];

    private final ClickedListener[] mButtonListeners = new ClickedListener[MAX_BUTTON_NUMBER];

    private String mTitle;

    private String mContent;

    private int mIconId;

    private int buttonNum = 0;

    private float dim = -1f;

    private boolean mOutsideTouchClosable = false;
    private boolean mMovable = false;

    private CommonDialog mCommonDialog;

    public void show()
    {
        if (mCommonDialog != null)
        {
            mCommonDialog.show();
            if (dim >= 0) {changeDialogDim(mCommonDialog, dim);}
        }
    }

    public void remove(){
        if (mCommonDialog != null) {mCommonDialog.destroy();}
    }

    private void changeDialogDim(CommonDialog dialog, float dim)
    {
        Optional<WindowManager.LayoutConfig> configOpt = dialog.getWindow().getLayoutConfig();
        configOpt.ifPresent(
                config ->
                {
                    config.dim = dim;
                    dialog.getWindow().setLayoutConfig(config);
                });
    }

    public interface ClickedListener
    {
        void onClick(GeneralDialog dialog);
    }

    public static class Builder
    {
        private final GeneralDialog mGeneralDialog;

        private final Context mContext;

        public Builder(Context context)
        {
            mContext = context;
            mGeneralDialog = new GeneralDialog();
        }

        public Builder setTitle(String title)
        {
            mGeneralDialog.mTitle = title;
            return this;
        }

        public Builder setContent(String content)
        {
            mGeneralDialog.mContent = content;
            return this;
        }

        public Builder setIcon(int id)
        {
            mGeneralDialog.mIconId = id;
            return this;
        }

        public Builder setDim(float dim)
        {
            if (dim > 1) { mGeneralDialog.dim = 1; }
            else if (dim < 0) { mGeneralDialog.dim = 0; }
            else { mGeneralDialog.dim = dim; }
            return this;
        }

        public Builder setOutsideTouchClosable(boolean closable)
        {
            mGeneralDialog.mOutsideTouchClosable = closable;
            return this;
        }

        public Builder setMovable(boolean movable)
        {
            mGeneralDialog.mMovable = movable;
            return this;
        }

        public Builder addButton(String text, int type, ClickedListener listener)
        {
            if (mGeneralDialog.buttonNum >= MAX_BUTTON_NUMBER) { return this; }
            mGeneralDialog.mButtonTexts[mGeneralDialog.buttonNum] = text;
            mGeneralDialog.mButtonTypes[mGeneralDialog.buttonNum] = type;
            mGeneralDialog.mButtonListeners[mGeneralDialog.buttonNum] = listener;
            mGeneralDialog.buttonNum++;
            return this;
        }

        public GeneralDialog create()
        {
            CommonDialog sDialog = new CommonDialog(mContext);
            sDialog.setSize(AttrHelper.vp2px(DIALOG_WIDTH_VP, mContext), ComponentContainer.LayoutConfig.MATCH_CONTENT);
            sDialog.setAlignment(LayoutAlignment.CENTER);
            sDialog.setTransparent(true);
            sDialog.setContentCustomComponent(initDialog());
            sDialog.setAutoClosable(mGeneralDialog.mOutsideTouchClosable);
            sDialog.setMovable(mGeneralDialog.mMovable);
            mGeneralDialog.mCommonDialog = sDialog;
            return mGeneralDialog;
        }

        private Component initDialog()
        {
            Component dialogLayout = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_general_dialog, null, false);
            dialogLayout.setBackground(new ShapeElement(){{setRgbColor(RgbColor.fromArgbInt(ResourceTool.getColor(mContext,ResourceTable.Color_dawnlighti_dialog_bg,0xEEEBE8)));setCornerRadius(ResourceTool.getFloat(mContext,ResourceTable.Float_dialog_corner_radius,0));}});

            Text textTitle = (Text) dialogLayout.findComponentById(ResourceTable.Id_title);
            if (mGeneralDialog.mTitle != null)
            {
                textTitle.setText(mGeneralDialog.mTitle);
                textTitle.setVisibility(Component.VISIBLE);
            }

            Image imageIcon = (Image) dialogLayout.findComponentById(ResourceTable.Id_icon);
            if (mGeneralDialog.mIconId != 0)
            {
                imageIcon.setPixelMap(mGeneralDialog.mIconId);
                imageIcon.setVisibility(Component.VISIBLE);
                Component titleLayout = dialogLayout.findComponentById(ResourceTable.Id_title_layout);
                titleLayout.setVisibility(Component.HIDE);
            }

            Text textContent = (Text) dialogLayout.findComponentById(ResourceTable.Id_content);
            Component bottomLayout = dialogLayout.findComponentById(ResourceTable.Id_bottom_layout);
            if (mGeneralDialog.mContent != null)
            {
                textContent.setText(mGeneralDialog.mContent);
                textContent.setVisibility(Component.VISIBLE);
                bottomLayout.setVisibility(Component.VISIBLE);
            }

            DirectionalLayout operationLayout = (DirectionalLayout) dialogLayout.findComponentById(ResourceTable.Id_operation_layout);
            float totalWeight = 1.25f;
            for (int i = 0; i < mGeneralDialog.buttonNum; i++)
            {
                if (i == 0)
                {
                    operationLayout.setVisibility(Component.VISIBLE);
                    bottomLayout.setVisibility(Component.VISIBLE);
                }
                Button button = new Button(mContext);
                operationLayout.addComponent(button);
                DirectionalLayout.LayoutConfig config = new DirectionalLayout.LayoutConfig();
                config.height = (int) ResourceTool.getFloat(mContext, ResourceTable.Float_button_height, 0);
                config.width = DirectionalLayout.LayoutConfig.MATCH_PARENT;
                config.weight = 1;
                int spacingHorizontal = (int) ResourceTool.getFloat(mContext, ResourceTable.Float_spacing_mini, 0);
                config.setMargins(spacingHorizontal, 0, spacingHorizontal, 0);
                button.setLayoutConfig(config);
                if (i != 0) { totalWeight = i + 1.0f; }
                button.setBackground(new GeneralButtonElement(mContext, mGeneralDialog.mButtonTypes[i]));
                if (mGeneralDialog.mButtonTypes[i] == GeneralButtonElement.TYPE_NORMAL) { button.setTextColor(new Color(ResourceTool.getColor(mContext, ResourceTable.Color_dawnlighti_button_text_light, 0xE5000000))); }
                else { button.setTextColor(new Color(ResourceTool.getColor(mContext, ResourceTable.Color_dawnlighti_button_text_accent, 0xFFFFFF))); }
                button.setText(mGeneralDialog.mButtonTexts[i]);
                int currentIndex = i;
                button.setClickedListener(component -> mGeneralDialog.mButtonListeners[currentIndex].onClick(mGeneralDialog));
                button.setFont(Font.DEFAULT_BOLD);
                button.setTextSize((int) ResourceTool.getFloat(mContext, ResourceTable.Float_button_size, 0));
                button.setMultipleLine(false);
                button.setTruncationMode(Text.TruncationMode.ELLIPSIS_AT_END);
                button.setPaddingLeft(spacingHorizontal);
                button.setPaddingRight(spacingHorizontal);
            }
            operationLayout.setTotalWeight(totalWeight);
            return dialogLayout;
        }
    }
}
